require('./myAccount.css')
require('../../common/header/index.js')
var _util=require('../../../util/util.js')
var _account_service=require('service/account-service.js')
var $=require('jquery')

var _myAccount={
    init: function (param) {
        this.loadInfo();
        this.bindEvents();
        return this;
      },

    loadInfo: function(param){
        // 初次加载页面时请求数据
        _account_service.getMyAccount(
            function ({data:res}) {
                $('#firstName').val(res.firstName);
                $('#lastName').val(res.lastName)
                $('#email').val(res.email)
                $('#phone').val(res.phone)
                $('#address1').val(res.address1)
                $('#address2').val(res.address2)
                $('#city').val(res.city)
                $('#state').val(res.state)
                $('#zip').val(res.zip)
                $('#country').val(res.country)

                $('#languagePreference').val(res.languagePreference)
                $('#favouriteCategoryId').val(res.favouriteCategoryId)
                $('#listOption').attr('checked',res.listOption)
                $('#bannerOption').attr('checked',res.bannerOption)
              },
            function (errMsg) {  
                $('#errMsg').text(errMsg.msg)
             }
        )

    },

    bindEvents:function (param) {  
        var _this=this;
        $('#newAccount').on('click',function (param) {
            var data={
                firstName:$.trim($('#firstName').val()),
                lastName:$.trim($('#lastName').val()),
                email:$.trim($('#email').val()),
                phone:$.trim($('#phone').val()),
                address1:$.trim($('#address1').val()),
                address2:$.trim($('#address2').val()),
                city:$.trim($('#city').val()),
                state:$.trim($('#state').val()),
                zip:$.trim($('#zip').val()),
                country:$.trim($('#country').val()),

                languagePreference:$.trim($('#languagePreference').val()),
                favouriteCategoryId:$.trim($('#favouriteCategoryId').val()),
                listOption:$('#listOption').get(0).checked,
                bannerOption:$('#bannerOption').get(0).checked
                };
            var result= _this.checkFormData(data);
            if(!result.status){
                 $('#errMsg').text(result.msg);
                return;
            };
             $('#errMsg').hide();
            _account_service.updateAccount(
                data,
                function(res){
                    window.location.reload()
                },
                function (errMsg) {
                    $('#errMsg').text(errMsg.msg);
                    $('#errMsg').show();
                  }
                )
            
          })
    },
    checkFormData: function(data){
        var result={
            status:false,
            msg:''
        };
        if(!data.firstName){
            result.msg='First name不能为空';
            return result;
        }
        else if(!data.email){
            result.msg='邮箱不能为空';
            return result;
        }
        else if(!data.phone){
            result.msg='手机号不能为空';
            return result;
        }
        else if(!data.address1&&!data.address2){
            result.msg='至少要填写一个地址不能为空';
            return result;
        }
        result.status=true;
        result.msg='初步通过验证';
        return result;
    }
}

module.exports=_myAccount.init()
